什麼是Adapter呢?
adapter目的是產生在activity裡某些元件內部資料,且額外獨立出來一處,專門撰寫產生資料過程的地方,避免都把程式打在activty造成程式過長。
常跟adapter一起用的元件有
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
for (int i = 0; i < 20; i++) {
HashMap<String, String> hashMap = new HashMap<>();
arrayList.add(hashMap);
}
其實除了 arrayAdapter的arraylist 還有其他的adapter
在MainActivity的XML新建一個listview
在MainAdapter的XML建立單一筆資料所需要顯示的內容
ListView listView;
這裡加入一個變數綁定元件並與產生資料的程式做連結MainAdapter mainAdapter;
設一個自定義的變數是在套用我們的MainAdaptermainAdapter = new MainAdapter(this, arrayList);
將arrayList從當前(mainActivity)丟到MainAdapter
public class MainActivity extends AppCompatActivity {
ListView listView;
MainAdapter mainAdapter;
static String[] data = {"abc", "123", "48763", "hello world", "apple"};
ArrayList<HashMap<String, String>> arrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 20; i++) {
HashMap<String, String> hashMap = new HashMap<>();
hashMap.put("number", String.format("%02d", i + 1));
hashMap.put("data", "data: " + data[new Random().nextInt(5)]);
arrayList.add(hashMap);
}
listView = findViewById(R.id.listView);
mainAdapter = new MainAdapter(this, arrayList);
listView.setAdapter(mainAdapter);
}
}
public class MainAdapter extends BaseAdapter {
private Context context;
private ArrayList<HashMap<String, String>> arrayList;
public MainAdapter(Context context, ArrayList<HashMap<String, String>> arrayList) {
this.context = context;
this.arrayList = arrayList;
}
@Override
public int getCount() {
return arrayList.size();
}
@Override
public Object getItem(int position) {
return arrayList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(context).inflate(R.layout.activity_main_adapter, parent, false);
ViewHolder holder = new ViewHolder();
holder.tx1 = convertView.findViewById(R.id.text1);
holder.tx2 = convertView.findViewById(R.id.text2);
convertView.setTag(holder);
holder.tx1.setText(arrayList.get(position).get("number"));
holder.tx2.setText(arrayList.get(position).get("data"));
return convertView;
}
private static class ViewHolder {
TextView tx1, tx2;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainAdapter">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp" />
</LinearLayout>
結果畫面如下